﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EFCoreRepository.DbContexts;
using EFCoreRepository.Extensions;
using EFCoreRepository.Repositories;
using Microsoft.EntityFrameworkCore;
using Zoomtel.Entity.BIZ;
using Zoomtel.Persist.BIZ.Models;
using Zoomtel.Persist.Query;

namespace Zoomtel.Persist.BIZ
{
    public class AgentInfoRepository : RepositoryAb<AgentInfoEntity>
    {
        public AgentInfoRepository(DefaultDbContext context) : base(context)
        {
        }
        public Task<AgentInfoEntity> GetByWechatId(string WechatId)
        {
            return dbSet.Where(a => a.AgentWechatId == WechatId).FirstOrDefaultAsync();
        }

        public async Task<AgentInfoPubModel> GetAgentInfo(string Uid)
        {
            var changeBSet = DbContext.Set<Entity.BIZ.ChangeBalanceEntity>();
      

            var entity=await dbSet.Where(a => a.Id == Uid).FirstOrDefaultAsync();

            AgentInfoPubModel model = new AgentInfoPubModel();
            model.AccountBalance = entity.AccountBalance;
            model.UpperId = entity.UpperId;
            model.Id = entity.Id;
            model.LockBalance = entity.LockBalance;
            model.Province = entity.Province;
            model.City = entity.City;
            model.Avatar = entity.Avatar;
            model.AgentPhoneNum = entity.AgentPhoneNum;
            model.AgentName = entity.AgentName;
            model.CreatedTime = entity.CreatedTime;
            model.CreateIP = entity.CreateIP;


            model.AllEarnings =Convert.ToDecimal( DbContextExtensions.ExecuteScalar(DbContext, "  select sum(Amount) from T_BIZ_CHANGEBALANCE where type=@type and AgentId=@AgentId", new {type=ChangeType.Separate.ToString(),AgentId=Uid }));//总收益

            model.YesterdayEarnings = Convert.ToDecimal(DbContextExtensions.ExecuteScalar(DbContext, "select count(1) from [T_BIZ_CASHLOG] where PassTime>'"+DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")+"' and PassTime<'"+ DateTime.Now.ToString("yyyy-MM-dd")+ "' and uid=@AgentId", new { AgentId = Uid })) ;//今日预期收益;//昨日收益
            model.ToDayProjectedEarnings =Convert.ToDecimal( DbContextExtensions.ExecuteScalar(DbContext, "select count(1) from T_BIZ_ITEMPROMOTION where ClickType=20 and SettleFlag=0 and AgentId=@AgentId", new { AgentId=Uid }))*0.8M;//今日预期收益

            return model;
        }

        public async Task<QueryResultModel<AgentInfoEntity>> Query(AgentQueryModel model)
        {
            var paging = model.Paging();
            var query = dbSet.AsQueryable();
            
            if (model.Status != null && !"".Equals(model.Status) && !"-1".Equals(model.Status))
            {
                query = query.Where(a => a.Status == model.Status);
            }
            if (model.AgentInfo != null && !"".Equals(model.AgentInfo))
            {
                query = query.Where(a => a.AgentCode.Contains(model.AgentInfo) || a.AgentName.Contains(model.AgentInfo));
            }
            if (!paging.OrderBy.Any())
            {
                query = query.OrderByDescending(m => m.CreatedTime);
            }
            return await query.PaginationGetResult(paging);
        }



        public Task<List<AgentInfoEntity>> GetProfitRank()
        {
            var query = dbSet.AsQueryable();
            return query.OrderByDescending(m => m.AccountBalance).Skip(0).Take(10).ToListAsync();


        }

        public AgentInfoEntity QueryEntity(string id) {
            var query = (from item in dbSet where item.Id == id
                         select item);
            return query.FirstOrDefault();
        }
        
    }
}
